//
//  SchoolRankingViewController.m
//  HotWord
//
//  Created by 贺 欢 on 13-3-3.
//
//

#import "SchoolRankingViewController.h"
#import "SchoolSelectCell.h"
#import "SchoolSelectViewController.h"
#import "HeaderTitleView.h"

@interface SchoolRankingViewController ()<SchoolSelectViewControllerDelegate>
{
    NSDictionary *_schoolRankingDic;
    NSString *_selectSchool;
}
@property (retain, nonatomic) IBOutlet UITableView *schoolTableView;

@end

@implementation SchoolRankingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _schoolRankingDic = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"School_Ranking_List" ofType:@"plist"]];
    }
    return self;
}

- (void)dealloc
{
    [_schoolTableView release];
    [_schoolRankingDic release];
    [_selectSchool release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [self setSchoolTableView:nil];
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _schoolTableView.top = self.view.height;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.4
                     animations:^{
                         _schoolTableView.top = 0;
                         [self.view viewWithTag:10].alpha = 1;
                     }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selectIndexPath = [_schoolTableView indexPathForSelectedRow];
    if (selectIndexPath)
    {
        [_schoolTableView deselectRowAtIndexPath:selectIndexPath animated:YES];
    }
    [self.view viewWithTag:10].alpha = 0;
}

#pragma mark - IBAction

- (IBAction)tapOnBackBtn:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(schoolRankingViewController:didSelectSchool:)])
    {
        [_delegate schoolRankingViewController:self didSelectSchool:_selectSchool];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return [COMPLEX_RANKING_KEYS count];
    }
    else
    {
        return [ARTS_RANKING_KEYS count];
    }
    
    return 0;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderTitleView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderTitleView" owner:self options:nil] objectAtIndex:0];
    if (section == 0)
    {
        headerView.titleLabel.text = @"综合大学";
    }
    else
    {
        headerView.titleLabel.text = @"文理学院";
    }
    
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SchoolSelectCell";
    SchoolSelectCell *cell = (SchoolSelectCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SchoolSelectCell" owner:self options:nil] objectAtIndex:0];
        cell.type = SchoolSelectCellTypeRanking;
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    NSArray *rankingList = nil;
    if (indexPath.section == 0)
    {
        rankingList = COMPLEX_RANKING_KEYS;
    }
    else
    {
        rankingList = ARTS_RANKING_KEYS;
    }
    
    [cell setTitle:[rankingList objectAtIndex:indexPath.row]];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *schoolType = nil;
    NSString *ranking = nil;
    
    if (indexPath.section == 0)
    {
        schoolType = @"综合大学";
        ranking = [COMPLEX_RANKING_KEYS objectAtIndex:indexPath.row];
    }
    else
    {
        schoolType = @"文理学院";
        ranking = [ARTS_RANKING_KEYS objectAtIndex:indexPath.row];
    }


    NSDictionary *schoolsDic = [_schoolRankingDic objectForKey:schoolType];
    NSArray *schools = [schoolsDic objectForKey:ranking];
    
    SchoolSelectViewController *schoolSelectViewController = [[SchoolSelectViewController alloc] initWithSchools:schools];
    schoolSelectViewController.headerTitle = [NSString stringWithFormat:@"%@：%@",schoolType,ranking,nil];
    schoolSelectViewController.delegate = self;
    [self.navigationController pushViewController:schoolSelectViewController animated:YES];
    [schoolSelectViewController release];
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

#pragma mark - SchoolSelectViewControllerDelegate

- (void)schoolSelectViewController:(SchoolSelectViewController *)viewController didSelectSchool:(NSString *)school
{
    if (_selectSchool)
    {
        RELEASE_SAFELY(_selectSchool);
    }
    
    _selectSchool = [school retain];
}


@end
